package h0907;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
  * @description 合并区间
  * @author 不知名帅哥
  * @date 2024/9/7 22:37
  * @version 1.0
*/
public class MergeIntervals {
    public static void main(String[] args) {

    }
    public int[][] merge(int[][] intervals) {
        List<int[]> res=new ArrayList<>();
        //按照起始时间排序
        Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
        int start=intervals[0][0];
        int end=intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            int curStart = intervals[i][0];
            int curEnd = intervals[i][1];
            if (end<curStart){
                res.add(new int[]{start,end});
                start=curStart;
                end=curEnd;
            }else {
                if (end<curEnd){
                    end=curEnd;
                }
                //end大于等于，则不变
            }
        }
        res.add(new int[]{start,end});
        return res.toArray(new int[res.size()][]);
    }
}
